<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;

/**
 * 权限控制器
 */
class RbacController  extends Controller {

    public function actionInit(){
        $auth = Yii::$app->authManager;

        // 建立borrowwaittrial权限
        $createPost = $auth->createPermission('borrowwaittrial');
        $createPost->description = 'borrow/wait-trial';
        $auth->add($createPost);

        // 建立borrowindex权限
        $updatePost = $auth->createPermission('borrowindex');
        $updatePost->description = 'borrow/index';
        $auth->add($updatePost);

        //  建立super_admin的角色
        $admin = $auth->createRole('super_admin');
        $auth->add($admin);
        
        //给super_admin分配borrowwaittrial和borrowindex的权限
        $auth->addChild($admin, $updatePost);
        $auth->addChild($admin, $createPost);

        //给用户user_id=1的用户分配super_admin的角色
        $auth->assign($admin, 1);        
    }
}
